import {
	createStore
} from "vuex"

const store = {
	state: {
		filteredItems: [], //购物车数组
		orderItems: [], //购物订单数组
		number: 0, //购物物品总量
		total_star: 0, //购物物品总星星
		pay_number: 0, //付款物品总量
		pay_star: 0, //付款物品总星星
		remain_star: 100, //星星余额
		token: '',
		children: {},
		isLogin: false,
	},
	mutations: {
		// setFilteredItemsFromComputed(state) {
		// 	state.filteredItems = this.getters.filteredItems;
		// },
		// addCart({commit, state}, item) {
		// 		  const list = [...state.filteredItems, item]
		// 		  commit("setFilteredItems", list)
		// },
		setFilteredItems(state, list) {
			state.filteredItems = list
		},
		setOrderItems(state, list) {
			state.orderItems = list
		},
		updateNumber(state, number) {
			state.number = number;
		},
		updateStar(state, total_star) {
			state.total_star = total_star;
		},
		updatePayNumber(state, number) {
			state.pay_number = number;
		},
		updatePayStar(state, total_star) {
			state.pay_star = total_star;
		},
		updateRemainStar(state, remain_star) {
			state.remain_star = remain_star;
		},
		updateToken(state, token) {
			state.token = token;
		},
		updateChildren(state, children) {
			state.children = children;
		},
		updateLogin(state, isLogin) {
			state.isLogin = isLogin;
		},
	},
	actions: {
		// updateFilteredItems({ commit, state }) {
		//   const filteredItems = state.items.filter((item) => item.status === 1);
		//   commit("setFilteredItems", filteredItems);
		// },

		updateOrder({
			commit,
			state
		}) {
			commit("setOrderItems", state.filteredItems)
		},
		addCart({
			commit,
			state
		}, item) {
			const list = [...state.filteredItems, item]
			commit("setFilteredItems", list)
		},
		deleteCart({
			commit,
			state
		}, item) {
			const list = state.filteredItems.filter((i) => i !== item);
			commit("setFilteredItems", list);
		},
		// addOrder({commit, state}, item) {
		// 	const list = [...state.orderItems, item]
		// 	commit("setOrderItems", list)
		// },
		deleteOrder({
			commit,
			state
		}, item) {
			const list = state.orderItems.filter((i) => i !== item);
			commit("setOrderItems", list);
		},
		updateNumber({
			commit,
			state
		}, number) {
			commit("updateNumber", number);
		},
		addNumber({
			commit,
			state
		}) {
			const new_number = state.number + 1;
			commit("updateNumber", new_number);
		},
		deleteNumber({
			commit,
			state
		}, item) {
			const new_number = state.number - item.buy_Quantity;
			commit("updateNumber", new_number);
		},
		updateStar({
			commit,
			state
		}, total_star) {
			commit("updateStar", total_star);
		},

		addStar({
			commit,
			state
		}, item) {
			const new_star = state.total_star + item.star;
			commit("updateStar", new_star);
		},
		deleteStar({
			commit,
			state
		}, item) {
			const new_star = state.total_star - item.star;
			commit("updateStar", new_star);
		},
		deleteAllStar({
			commit,
			state
		}, item) {
			const new_star = state.total_star - item.star_Quantity;
			commit("updateStar", new_star);
		},
		updatePayNumber({
			commit,
			state
		}) {
			commit("updatePayNumber", state.number);
		},
		deletePayNumber({
			commit,
			state
		}, item) {
			const new_number = state.pay_number - item.buy_Quantity;
			commit("updatePayNumber", new_number);
		},
		updatePayStar({
			commit,
			state
		}) {
			commit("updatePayStar", state.total_star);
		},
		deletePayStar({
			commit,
			state
		}, item) {
			const new_star = state.pay_star - item.star_Quantity;
			commit("updatePayStar", new_star);
		},
		payOne({
			commit,
			state
		}, item) {
			const new_remainStar = state.remain_star - item.star_Quantity;
			commit("updateRemainStar", new_remainStar);
		},
		payForAll({
			commit,
			state
		}) {
			const new_remainStar = state.remain_star - state.pay_star;
			commit("updateRemainStar", new_remainStar);
		},
		emptyCart({
			commit,
			state
		}) {
			commit("setOrderItems", []);
			commit("updatePayNumber", 0);
			commit("updatePayStar", 0);
		},
		askToken({
			commit,
			state
		}, token) {
			commit("updateToken", token);
		},
		updateChildren({
			commit,
			state
		}, children) {
			commit("updateChildren", children);
		},
		updateLogin({
			commit,
			state
		}, isLogin) {
			commit('updateLogin', isLogin);
		},
	},
}

export default createStore(store)